<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键词" prop="key">
        <el-input
          v-model="queryParams.keyword"
          placeholder="集群、域名..."
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格展示DNS服务商数据 -->
    <el-table v-loading="loading" :data="providersList" @selection-change="handleSelectionChange">
      <el-table-column label="集群" align="center" prop="clusterName"/>
      <el-table-column label="子域名" align="center" prop="dnsDomain"/>
      <el-table-column label="DNS服务商" align="center" prop="providerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.edge_dns_providers" :value="scope.row.providerType"/>
        </template>
      </el-table-column>
      <el-table-column label="DNS服务商账号" align="center" prop="accountDescription"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleInfo(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getClusterDnsInfoList"
    />

  </div>
</template>

<script>
import {listProviders, getProviders, delProviders, addProviders, updateProviders} from "@/api/dns/providers";
import {clusterDnsInfoList} from "@/api/nodes/clusters";

export default {
  dicts: ['edge_dns_providers'],
  name: "Providers",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      providersList: [],
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
      },
      form: {
        providerName: '',
        providerType: '',
        apiUrl: '',
        apiKey: '',
        minTTL: null, // 最小TTL（秒）
        createdBy: '',
        createdTime: null
      },
      rules: {

      }
    };
  },
  created() {
    this.getClusterDnsInfoList();
  },
  methods: {
    // 获取集群域名解析信息
    getClusterDnsInfoList(){
      clusterDnsInfoList(this.queryParams).then(response => {
        this.providersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleInfo(row){
      // 跳转二级路由
      this.$router.push({ name: 'ClusterDnsInfo', params: { id: row.clusterId } }).catch(() => {});
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        providerName: '',
        providerType: '',
        apiUrl: '',
        apiKey: '',
        minTTL: 60,
        createdBy: '',
        createdTime: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getClusterDnsInfoList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>

<style scoped>
.el-form-item__helper {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
  display: block;
}
</style>

